#include "linked_common.h"
#include "sys_common.h"

//定义学生结构体
typedef struct Student
{
    //唯一标识符
    int id;
    //char类型数组，存储学号
    char *sno;
    //char类型数组，存储姓名
    char *sname;
    //gender性别，0是女，1是男
    int gender;
    //char类型数组，存储地址
    char *saddress;
}Student;

LinkedList stulist;
struct Student s1={1,"s001","张三",1,"成都市金牛区"};
struct Student s2={2,"s002","李四",0,"成都市武侯区"};
struct Student s3={3,"s003","王五",0,"成都市高新区"};
struct Student s4={4,"s004","尼古拉",1,"成都市新都区"};
struct Student s5={5,"s005","宁红叶",0,"重庆市渝北区"};
struct Student s6={6,"s006","孙悟空",1,"上海市浦东新区"};
//定义初始化学员函数
void init_student_array(){
    //把实例保存到学生链表中
    append(&stulist,&s1);
    append(&stulist,&s2);
    append(&stulist,&s3);
    append(&stulist,&s4);
    append(&stulist,&s5);
    append(&stulist,&s6);
}

//定义查看学员函数
void show_student(){
    printf("ID\t学号\t姓名\t性别\t地址\n");
    Node *node=stulist.head;
    while (node)
    {
        Student *a=node->data;
        char *gender=a->gender?"男":"女";
        printf("%d\t%s\t%s\t%s\t%s\n",a->id,a->sno,a->sname,gender,a->saddress);
        node=node->next;
    }
    //继续展示菜单
    show_menu();
}

//定义ID查询信息函数
void show_student_id(){
    printf("请输入学员的ID：\n");
    //定义一个变量接受输入的id
    int opr_id=0;
    scanf("%d",&opr_id);
    Student *s=get_student_index(opr_id);
    if (s->id!=0)
    {   
        printf("ID\t学号\t姓名\t性别\t地址\n");
        char *gender=s->gender?"男":"女";
        printf("%d\t%s\t%s\t%s\t%s\n",s->id,s->sno,s->sname,gender,s->saddress);
    }
    else
    {
        printf("暂时没有该学号记录...\n");
    }
    //显示菜单给用户继续操作
    show_menu();
}

//获取下标函数
struct Student* get_student_index(int id){
    Node *node=stulist.head;
    while (node)
    {
        Student *a=node->data;
        if (a->id==id)
        {
            return a;
        }
        node=node->next;
    }
    printf("id=%d的用户不存在！\n",id);
    //没查到返回-1
    show_menu();
}

//修改学员信息
void update_student(){
    printf("请输入要修改的学员的ID：\n");
    //定义一个变量接受输入的id
    int id=0;
    scanf("%d",&id);
    Node *node=stulist.head;
    while (node)
    {
        Student *a=node->data;
        if (a->id==id)
        {
            printf("请输入学号 姓名 性别 地址 用空格隔开\n");
            char sno[10];
            char sname[50];
            int gender;
            char saddress[100];
            scanf("%s %s %d %s",sno,sname,&gender,saddress);
            Student *stu=a;
            stu->sno=sno;
            stu->sname=sname;
            stu->gender=gender;
            stu->saddress=saddress;
            show_student();
            return;
        }
        node=node->next;
        
    }
    printf("id=%d的用户不存在！\n",id);
    show_menu();
}

//根据id删除学员信息
void delete_student(){
    printf("请输入要删除的学员的ID：\n");
    //定义一个变量接受输入的id
    int id=0;
    scanf("%d",&id);
    Node *node=stulist.head;
    while (node)
    {
        Student *a=node->data;
        if (a->id==id)
        {
            delete_list(&stulist,node);
            show_student();
            return;
        }
        node=node->next;
    }
    printf("id=%d的用户不存在！\n",id);
    show_menu();
}

//新增学员信息
void add_student(){
    printf("请输入学号 姓名 性别 地址 用空格隔开\n");
        char sno[10];
        char sname[50];
        int gender;
        char saddress[100];
        scanf("%s %s %d %s",sno,sname,&gender,saddress);
        Student *s_tail=(Student *)(stulist.tail->data);
        int id=s_tail->id+1;
        Student *new_s=(Student *)malloc(sizeof(Student));
        new_s->id=id;
        new_s->sno=sno;
        new_s->sname=sname;
        new_s->gender=gender;
        new_s->saddress=saddress;

        append(&stulist,new_s);
        show_student();
}